package com.ssyc.gsk_parents.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;
import com.ssyc.common.base.NineImageLoader;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.view.MyListView;
import com.ssyc.gsk_parents.R;
import com.ssyc.gsk_parents.bean.QuanZiInfo;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 圈子适配器
 * Created by Administrator on 2018/6/25 0025.
 */

public class QuanZiRvAdapter extends BaseQuickAdapter<QuanZiInfo, BaseViewHolder> {

    private Activity activtiy;

    private RecyclerView recyclerView;

    private Context context;

    private ArrayList<ImageInfo> imageInfo;


    //弹框相关的PopUpWindow
    private PopupWindow popupWindow;
    private View view;
    private TextView tvZan, tvPl, tvZf;


    public QuanZiRvAdapter(RecyclerView recyclerView, Activity activtiy, Context context, int layoutResId, @Nullable List<QuanZiInfo> data) {
        super(layoutResId, data);
        this.activtiy = activtiy;
        this.context = context;
        this.recyclerView = recyclerView;

        //模拟九宫格数据
        initNineData();
    }

    /**
     * 模拟九宫格图片数据的方法
     */
    private void initNineData() {
        imageInfo = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            ImageInfo info = new ImageInfo();
            info.setThumbnailUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1530520084&di=661f9679e82643bd79dac6749e4b9614&imgtype=jpg&er=1&src=http%3A%2F%2Fwww.qqkubao.com%2Fuploadfile%2F2014%2F10%2F1%2F20141011193154378.jpg");
            info.setBigImageUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1529925473534&di=4e97bd159d225568f081dde1a77021fc&imgtype=0&src=http%3A%2F%2Ff6.topitme.com%2F6%2F50%2F75%2F113531694627575506o.jpg");
            imageInfo.add(info);
        }
    }

    @Override
    protected void convert(BaseViewHolder holder, QuanZiInfo item) {
        //最外层布局
        LinearLayout llOut = holder.getView(R.id.ll_out);
        //头像
        ImageView lvHead = holder.getView(R.id.lv_head);

        //姓名
        TextView tvName = holder.getView(R.id.tv_name);

        //内容
        TextView tvContent = holder.getView(R.id.tv_content);

        //时间
        TextView tvTime = holder.getView(R.id.tv_time);

        //评论的图标
        final ImageView lvComment = holder.getView(R.id.lv_comment);


        lvComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击评论弹出评论的PopUpWindow
                view = View.inflate(context, R.layout.common_pop_comment, null);
                tvZan = view.findViewById(R.id.tv_zan);
                tvPl = view.findViewById(R.id.tv_pinglun);
                tvZf = view.findViewById(R.id.tv_zhuanfa);
                //点赞的点击事件
                tvZan.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                });
                //评论的点击事件
                tvPl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (popupWindow != null && popupWindow.isShowing()) {
                            popupWindow.dismiss();
                        }

                        //通过EventBus发送消息给Activity界面通知显示底部的评论布局和软键盘
                        EventBus.getDefault().post(new BusInfo("showcontent", holder.getLayoutPosition()));
                    }
                });
                //转发的点击事件
                tvZf.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                });
                popupWindow = new PopupWindow(view, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
                popupWindow.setTouchable(true);
                popupWindow.setFocusable(true);
                popupWindow.setBackgroundDrawable(new BitmapDrawable());

                view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
                int mShowMorePopupWindowWidth = view.getMeasuredWidth();
                int mShowMorePopupWindowHeight = view.getMeasuredHeight();

                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                } else {
                    int heightlvComment = lvComment.getHeight();

                    popupWindow.showAsDropDown(lvComment, -mShowMorePopupWindowWidth - 20,
                            -(mShowMorePopupWindowHeight + heightlvComment) / 2);
                }
            }
        });

        //九宫格展示图片
        NineGridView ngv = holder.getView(R.id.ninegv);
        ngv.setImageLoader(new NineImageLoader());
        ngv.setAdapter(new NineGridViewClickAdapter(context, imageInfo));

        //展示评论内容的ListView
        MyListView mlv = holder.getView(R.id.lv_content);

    }
}
